<template>
  <div>
    <div style="width: 100%;padding-left: 20px;padding-top: 15px;color: #3ea0f1;font-size: 14px">
      <span id="span-right">
        会诊状态：
        <el-select v-model="status" class="m-2" size="default">
          <el-option
              v-for="item in consultationStatus"
              :key="item.status"
              :label="item.dictLabel"
              :value="item.status"
          />
        </el-select>
      </span>

      <span id="span-right">
        病理类型：
        <el-select v-model="pathologyType" class="m-2" placeholder="请选择病理类型" style="width: 7%">
          <el-option
              key="0"
              label="全部"
              value="0"
          />
          <el-option
              v-for="item in pathologys"
              :key="item.id"
              :label="item.pathology"
              :value="item.id"
          />
        </el-select>
      </span>

      <span id="span-right">
        关键字：
        <el-input v-model="condition" placeholder="专家/病理号/姓名..." style="width: 200px"/>
      </span>

      <span id="span-right">
        诊断时间：
        <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </span>

      <span id="span-right">
        <el-button type="primary" icon="Search" @click="getCaseByHospital">搜索病例</el-button>
        <el-button type="primary" icon="Search" @click="exportToExcel">excel导出</el-button>
      </span>

    </div>

    <hr>

    <div>
      <el-table
          :data="tbd.list"
          style="width: 100%"
          v-loading="loading"
          @selection-change="handleSelectionChange"
          :cell-style="{ textAlign: 'center' }"
          :header-cell-style="{ 'text-align': 'center' }"
          stripe
      >
        <el-table-column type="index" width="50"/>
        <el-table-column property="emrCase.status" label="会诊状态" width="80"/>
        <el-table-column property="id" label="会诊编号" width="135"/>
        <el-table-column property="emrCase.pathology" label="病理类型" width="80"/>
        <el-table-column property="emrCase.caseNo" label="病理号" width="135"/>
        <el-table-column property="emrCase.name" label="病人姓名" width="130"/>
        <el-table-column property="emrCase.sex" label="性别" width="70"/>
        <el-table-column property="emrCase.age" label="年龄" width="70"/>
        <el-table-column property="emrCase.sendHospital" label="送检医院" show-overflow-tooltip/>
        <el-table-column prop="emrCase.application_date" label="申请时间" sortable show-overflow-tooltip>
          <template #default="scope">
            <span>{{ parseTime(scope.row.emrCase.application_date) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="emrCase.expertDate" label="诊断时间" sortable show-overflow-tooltip>
          <template #default="scope">
            <span>{{ parseTime(scope.row.emrCase.expertDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column property="emrCase.expertName" label="专家" width="80"/>
        <el-table-column property="emrCase.expertDiagnosis" label="诊断内容" show-overflow-tooltip/>
      </el-table>
      <div style="width: 100%;margin-top: 5px;border-bottom: 2px #EBEEF4 solid;position: fixed;left: 0;bottom: 0">
        <el-pagination
            style="display:flex;justify-content: center; align-items:center;padding-bottom: 5px"
            v-model:current-page="pages.pageNum"
            v-model:page-size="pages.pageSize"
            :page-sizes="[10, 19]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tbd.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
      </div>

      <el-dialog
          v-model="dialogVisible"
          title="提示"
          width="30%"
      >
        <span v-if="dialogStatus == 0">确定要分配吗？</span>
        <span v-if="dialogStatus == 1">确定要退回吗？</span>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="definiteAllocation" v-if="dialogStatus == 0">
          确定
        </el-button>
        <el-button type="primary" @click="returnSite" v-if="dialogStatus == 1">
          确定
        </el-button>
      </span>
        </template>
      </el-dialog>
    </div>

  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import * as triage from '@/api/triage/triage'
import * as frozen from '@/api/frozen/frozen'
import * as statistics from '@/api/statistics2/statistics'
import {ElMessage} from 'element-plus'
import {consultationReport} from "@/api/statistics2/statistics";
import * as XLSX from 'xlsx';


let pathologyid = ref('0')

const status = ref()

const pathologyType = ref()

const condition = ref()

const dateRange = ref([])

const consultationStatus = ref([])

let pathologys = ref([])

let caseNo = ref(null)

let name = ref(null)

//当前选中申请医院id
let hospitalId = ref('')

let countHospital = ref('')

let hospitals = ref([])

const tbd = ref([])

const loading = ref(true)

let pages = ref({})

let ids = ref([])

let dialogStatus = ref()

const dialogVisible = ref(false)

const returnSiteId = ref()

let sex = ref('')

/**
 * 批量分配
 * @param val当前选中行数据
 */
let handleSelectionChange = (val) => {
  let list = [];
  val.forEach((val) => {
    list.push(val.id)
  })
  ids.value = list;
}

/**
 * 修改dialog弹窗为"退回"
 * @param row
 */
function changeDialogStatus(row) {
  dialogStatus.value = 1
  dialogVisible.value = true
  returnSiteId.value = row.row.id;
}

/**
 * 退回
 */
function returnSite() {
  frozen.returnSite(returnSiteId.value).then(res => {
    tableSuccessTips(res);
    dialogVisible.value = false;
  })
}

const handleSizeChange = (val) => {
  pages.value.pageSize = val;
  getCaseByHospital(pages.value.pageNum, pages.value.pageSize)
}

const handleCurrentChange = (val) => {
  pages.value.pageNum = val;
  getCaseByHospital(pages.value.pageNum, pages.value.pageSize)
}


function getCountHospital(e) {
  let [name] = e;
  countHospital.value = name;
}

/**
 * 获取病理类型
 */
function getPathology() {
  triage.getPathology().then(res => {
    pathologys.value = res.data;
  })
}

function getConsultationStatus() {
  statistics.getConsultationStatus().then(res => {
    consultationStatus.value = res.data;
    console.log(consultationStatus.value);
  })
}

function getCaseByHospital() {
  console.log(status.value, pathologyType.value, condition.value, dateRange.value, pages.value.pageNum, pages.value.pageSize)
  statistics.consultationReport(status.value, pathologyType.value, condition.value, dateRange.value, pages.value.pageNum, pages.value.pageSize).then(res => {
    console.log(res);
    tbd.value = res.data;
    pages.value.pageNum = res.data.pageNum
    pages.value.pageSize = res.data.pageSize
    pages.value.size = res.data.size
    loading.value = false;
  })
}

function getCountCase(row) {
  dialogStatus.value = 0
  dialogVisible.value = true
  ids.value.push(row.row.id)
}

/**
 * 批量分配
 */
function definiteAllocation() {
  frozen.allotCaseSlide(ids.value).then(res => {
    tableSuccessTips(res)
    dialogVisible.value = false;
  })
}

function tableSuccessTips(res) {
  if (res.code == 200) {
    ElMessage({
      message: res.msg,
      type: 'success',
    })
    getCaseByHospital();
  } else if (res.code == 500) {
    ElMessage.error(res.msg)
  }
}

function exportToExcel(){
  // 获取表格数据
  const data = tbd.value.list;

  const tables = []


  data.forEach((item,index)=>{
    if (item.emrCase.sex == 1){
      sex.value = '男'
    }else {
      sex.value = '女'
    }

    const tableData = {
      status: item.emrCase.status,
      id: item.id,
      pathology: item.emrCase.pathology,
      caseNo: item.emrCase.caseNo,
      name: item.emrCase.name,
      sex: sex.value,
      age: item.emrCase.age,
      sendHospital: item.emrCase.sendHospital,
      application_date: item.emrCase.application_date,
      expertDate: item.emrCase.expertDate,
      expertName: item.emrCase.expertName,
      expertDiagnosis: item.emrCase.expertDiagnosis
    }
    tables.push(tableData)
  })
  // 创建一个工作簿
  const workbook = XLSX.utils.book_new();

  // 创建一个工作表
  const worksheet = XLSX.utils.json_to_sheet(Array.isArray(tables) ? tables : [tables]);

  // 将工作表添加到工作簿中
  XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');

  // 将工作簿保存为 Excel 文件
  XLSX.writeFile(workbook, 'table.xlsx');
}

getPathology();
getConsultationStatus();
getCaseByHospital();
</script>

<style scoped>
#span-right {
  padding-right: 20px;
}

.el-table--striped .el-table__body tr.el-table__row--striped.el-table__row--striped
.el-table__row--striped td {
  background-color: #eaf3fb;
}
</style>